﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Drawing;
using System.Windows.Forms;

namespace Tztek.GStudio.Interaction
{
    public class MoveControl
    {
        #region 实现窗体内的控件拖动
        //用法：在Form初始化或者Form_Load时先执行
        //DragComponent a = new DragComponent();
        //a.initProperty(groupBox1);
        //将界面groupBox1上的所有控件都绑定MyMouseDown、MyMouseLeave、MyMouseMove事件。 

        public delegate void OnRefashMap();
        public virtual event OnRefashMap eventRefashMap;

        private Control control;
        const int Band = 5;
        const int MinWidth = 10;
        const int MinHeight = 10;
        private EnumMousePointPosition m_MousePointPosition;
        private Point p, p1;

        private enum EnumMousePointPosition
        {
            MouseSizeNone = 0, //'无
            //MouseSizeRight = 1, //'拉伸右边框
            //MouseSizeLeft = 2, //'拉伸左边框
            //MouseSizeBottom = 3, //'拉伸下边框
            //MouseSizeTop = 4, //'拉伸上边框
            //MouseSizeTopLeft = 5, //'拉伸左上角
            //MouseSizeTopRight = 6, //'拉伸右上角
            //MouseSizeBottomLeft = 7, //'拉伸左下角
            //MouseSizeBottomRight = 8, //'拉伸右下角
            MouseDrag = 9 // '鼠标拖动
        }

        private void MyMouseDown(object sender, MouseEventArgs e)
        {
            p.X = e.X;
            p.Y = e.Y;
            p1.X = e.X;
            p1.Y = e.Y;
        }

        private void MyMouseLeave(object sender, EventArgs e)
        {
            m_MousePointPosition = EnumMousePointPosition.MouseSizeNone;
            control.Cursor = Cursors.Arrow;
        }

        private EnumMousePointPosition MousePointPosition(Size size, MouseEventArgs e)
        {
            if ((e.X >= -1 * Band) | (e.X <= size.Width) | (e.Y >= -1 * Band) | (e.Y <= size.Height))
            {
                return EnumMousePointPosition.MouseDrag;
            }
            else
            {
                return EnumMousePointPosition.MouseSizeNone;
            }
        }

        int i = 0;
        private void MyMouseMove(object sender, MouseEventArgs e)
        {
            i++;
            if (i % 5 == 0)
            {
                // Console.WriteLine(DateTime.Now.ToString("ss:fff") + " " + i + " " + e.Location.X + "  " + e.Location.Y);
                Control lCtrl = (sender as Control);
                if (e.Button == MouseButtons.Left)
                {
                    switch (m_MousePointPosition)
                    {
                        case EnumMousePointPosition.MouseDrag:
                            int top = lCtrl.Top + e.Y - p.Y;
                            int left = lCtrl.Left + e.X - p.X;
                            if (lCtrl.GetType().ToString() == "System.Windows.Forms.Panel")
                            {
                                lCtrl.Left = lCtrl.Left + e.X - p.X;
                                lCtrl.Top = lCtrl.Top + e.Y - p.Y;
                            }
                            else
                            {
                                if (left > 0 && left + lCtrl.Width <= control.Width)
                                {
                                    lCtrl.Left = lCtrl.Left + e.X - p.X;
                                }
                                if (top > 0 && top + lCtrl.Height <= control.Height)
                                {
                                    lCtrl.Top = lCtrl.Top + e.Y - p.Y;
                                }
                                lCtrl.BringToFront();
                            }
                         
                            //Console.WriteLine(lCtrl.Bottom + "  " + lCtrl.Right);
                            break;

                        default:
                            break;
                    }
                    if (lCtrl.Width < MinWidth) lCtrl.Width = MinWidth;
                    if (lCtrl.Height < MinHeight) lCtrl.Height = MinHeight;
                    //ucGTaskDesign uc = lCtrl as ucGTaskDesign;
                    //if (uc != null)
                    //{
                    //    uc.ShowP();
                    //}
                }
                else
                {
                    m_MousePointPosition = MousePointPosition(lCtrl.Size, e); //'判断光标的位置状态
                    switch (m_MousePointPosition) //'改变光标
                    {
                        case EnumMousePointPosition.MouseSizeNone:
                            control.Cursor = Cursors.Arrow;       //'箭头
                            break;
                        case EnumMousePointPosition.MouseDrag:
                            control.Cursor = Cursors.SizeAll;     //'四方向
                            break;

                        default:
                            break;
                    }
                }
            }
        }

        private void MyMouseUp(object sender, MouseEventArgs e)
        {
            if (null != eventRefashMap && eventRefashMap.GetInvocationList().Any())
            {
                eventRefashMap.BeginInvoke(null, null);
            }
        }

        public void initProperty(Control ctl)
        {
            control = ctl;
            for (int i = 0; i < control.Controls.Count; i++)
            {
                control.Controls[i].MouseDown -= new MouseEventHandler(MyMouseDown);
                control.Controls[i].MouseLeave -= new EventHandler(MyMouseLeave);
                control.Controls[i].MouseMove -= new MouseEventHandler(MyMouseMove);
                control.Controls[i].MouseUp -= new MouseEventHandler(MyMouseUp);
            }
            for (int i = 0; i < control.Controls.Count; i++)
            {
                control.Controls[i].MouseDown += new MouseEventHandler(MyMouseDown);
                control.Controls[i].MouseLeave += new EventHandler(MyMouseLeave);
                control.Controls[i].MouseMove += new MouseEventHandler(MyMouseMove);
                control.Controls[i].MouseUp += new MouseEventHandler(MyMouseUp);
            }
        }
        #endregion
    }
}
